$.WishWall2 = $.WishWall2 || {};


$.extend($.WishWall2,{
	ListButtonClick: bindListEvents,
	listPageSize:10,
	
	listWishes:function(sid, page){
		if(!$.WishWall2.validateRequest())
			return false;
			
		$.ajax({
			type: "get",
			//contentType: "application/json",
			dataType: "json",
			url: ajaxRoot + "WishHandler.ashx",
			data: { "op": "gets", "sid": Subject.id, "page": page, "size": $.WishWall2.listPageSize },
			timeout: 10000,
			cache: false,
			complete:function(){
				$.WishWall2.requestFinished();
			},
			success: function(msg) {
				$('div.list_content').empty();
				
				var wishes = msg.data.wishes;
				var count = msg.data.count;
				
				$.each(wishes, function(i,w) {
					$('div.list_content').jWishForList({
						id:w.wid,
						content:w.words,
						sender:w.sender,
						date:w.createTime
					});
				});

				//*------------- 生成分页控件 -----------------
				function PageClick(pageclickednumber) {
					$.WishWall2.listWishes(sid, pageclickednumber);
					//$("#result").html("Clicked Page " + pageclickednumber);
				}
				
				var pageSize = $.WishWall2.listPageSize;
				var pageCount = parseInt((count + pageSize - 1)/pageSize);
				$("#list_pager").pager({ pagenumber: page, pagecount: pageCount, buttonClickCallback: PageClick });
			}
		});	
	}
});


function bindListEvents() {
	// 添加纸条窗体
    $('li.list').click(function() {
        $.WishWall2.showMask();
        //$.WishWall2.showLoading();
        var $opWin = $('#operations');
        $opWin.load(appRoot + 'component/list.html', null, initList);

        function initList() {
            // 加载完成
            //$.WishWall2.hideLoading();
            //$('div.list_content').html('<table id="list_table"></table>');
            $opWin.show();
			$opWin.draggable('destroy');
			$opWin.draggable({
				handle:'div.list_title'
			});
			
			$('div.list_close').click(function(){
				$('div.wish-win').hide();
				$.WishWall2.hideMask();
			});
            //************** 初始化窗体 ****************
			$.WishWall2.listWishes(Subject.id, 1);
        }
    });	
}
